﻿using System.Collections.ObjectModel;
using Mvvm.Models;
using Mvvm.Services;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;
using ReactiveUI;
using CommunityToolkit.Mvvm.Input;
using System.Windows.Input;
using DynamicData;
using EmotionChat.Services;
using Mvvm.Views;

namespace Mvvm.ViewModels
{
    public class MainWindowViewModel : ViewModelBase
    {
        private readonly DepressionTestStorage _depressionTestStorage;
        // 用于存储 ComboBox 数据
        public ObservableCollection<ChatDataOption> ChatDataOptions { get; set; }
        private ChatDataOption _selectedChatData;
        public ChatDataOption SelectedChatData
        {
            get => _selectedChatData;
            set => this.RaiseAndSetIfChanged(ref _selectedChatData, value);
        }

        public MainWindowViewModel()
        {
            _depressionTestStorage = new DepressionTestStorage(); // 创建存储实例
            _chatDataProcessedStorage = new ChatDataProcessedStorage();
            var depressionTest = new DepressionTest(_depressionTestStorage); // 创建 DepressionTest 实例
            // Set current page to first on start up
            _CurrentPage = Pages[0];
            // Create Observables which will activate to deactivate our commands based on CurrentPage state
            var canNavNext = this.WhenAnyValue(x => x.CurrentPage.CanNavigateNext);
            var canNavPrev = this.WhenAnyValue(x => x.CurrentPage.CanNavigatePrevious);
            ChatDataOptions = new ObservableCollection<ChatDataOption>();
            _ = LoadChatDataOptions();
            NavigateNextCommand = ReactiveCommand.Create(NavigateNext, canNavNext);
            NavigatePreviousCommand = ReactiveCommand.Create(NavigatePrevious, canNavPrev);
        }
        private static readonly IXunFeiClient _xunFeiClient;
        private static readonly IChatDataStorage _chatDataStorage;
        private static IChatDataProcessedStorage _chatDataProcessedStorage;
        // A read.only array of possible pages
        private readonly PageViewModelBase[] Pages = 
        { 
            //这是首页
            new FirstPageViewModel(),
            new PersonalityProfileViewModel(),
            new DepressionTestViewModel(new DepressionTestStorage()) , // 传递正确的参数
            new DepressionAnalysisViewModel(_chatDataProcessedStorage, _xunFeiClient),
            new SentimentAnalysisViewModel(),
            new DirectoryViewModel(new ChatDataProcessedStorage(),
            new ChatTimeAIAdviceService(),
            new ChatTimeReportService(),
            new ChatDataStorage()),
            new ChatReportViewModel(new ChatDataStorage(),new ChatDataProcessedStorage()),
            //这里是功能5的页面

            //这里是功能6的页面
        };

        // The default is the first page
        private PageViewModelBase _CurrentPage;

        /// <summary>
        /// Gets the current page. The property is read-only
        /// </summary>
        public PageViewModelBase CurrentPage
        {
            get { return _CurrentPage; }
            private set { this.RaiseAndSetIfChanged(ref _CurrentPage, value); }
        }
        // 加载 chatProcessedData 数据并填充到 ComboBox 中
        private async Task LoadChatDataOptions()
        {
            var chatData = new ChatDataProcessedStorage();
            await chatData.InitializeAsync();
            var chatProcessedDataList = await chatData.ListAsync();
            // 如果为空，设置初始值
            if (chatProcessedDataList.Count == 0)
            {
                chatProcessedDataList.Add(new ChatProcessedData
                {
                    Identifier = 0,
                    Username1 = "当前暂无用户",
                    Username2 = "当前暂无用户",
                });
            }
            var options = chatProcessedDataList.Select(cd => new ChatDataOption
            {
                Identifier = cd.Identifier,
                User1 = cd.Username1,  // 假设每个 chatProcessedData 中有 User1 和 User2
                User2 = cd.Username2,
            }).ToList();

            // 将数据添加到 ObservableCollection
            foreach (var option in options)
            {
                ChatDataOptions.Add(option);
            }
        }
        /// <summary>
        /// Gets a command that navigates to the next page
        /// </summary>
        public ICommand NavigateNextCommand { get; }

        private void NavigateNext()
        {
            // get the current index and add 1
            var index = Pages.IndexOf(CurrentPage) + 1;

            // Be aware that we have no check if the index is valid. You may want to add it on your own.
            if (index < Pages.Length)
            {
                CurrentPage = Pages[index];
            }
        }

        /// <summary>
        /// Gets a command that navigates to the previous page
        /// </summary>
        public ICommand NavigatePreviousCommand { get; }

        private void NavigatePrevious()
        {
            // get the current index and subtract 1
            var index = Pages.IndexOf(CurrentPage) - 1;

            // Be aware that we have no check if the index is valid. You may want to add it on your own.
            if (index >= 0)
            {
                CurrentPage = Pages[index];
            }
            else
            {
                // If index goes out of bounds, navigate to first page
                CurrentPage = Pages[0];
            }
        }
        // 添加新的命令，用于导航到不同的页面
        public ICommand Function0Command => new RelayCommand(() => NavigateToPage(1));
        public ICommand Function1Command => new RelayCommand(() => NavigateToPage(2));
        public ICommand Function2Command => new RelayCommand(() => NavigateToPage(3));
        public ICommand Function3Command => new RelayCommand(() => NavigateToPage(4));
        public ICommand Function4Command => new RelayCommand(() => NavigateToPage(5));
        public ICommand Function5Command => new RelayCommand(() => NavigateToPage(6));
        public ICommand Function6Command => new RelayCommand(() => NavigateToPage(7));
        public ICommand BackMainViewCommand => new RelayCommand(() => NavigateToPage(0));
 

        private void NavigateToPage(int index)
        {
            if (index >= 0 && index < Pages.Length)
            {
                CurrentPage = Pages[index];
            }
            else
            {
                // Handle error (maybe log it or show default page)
                CurrentPage = Pages[0]; // If out of bounds, navigate to the first page
            }
        }

        public void NavigateToPage(PageViewModelBase page)
        {
            CurrentPage = page;
        }
        public class ChatDataOption
        {
            public int Identifier { get; set; }
            public string User1 { get; set; }
            public string User2 { get; set; }

            // 用于显示在 ComboBox 中的文本
            public string DisplayName => $"{User1} 与 {User2} - ID: {Identifier}";
        }
    }
}